package se.mbaeumer.fxtodo.core.model;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javafx.beans.property.IntegerProperty;
import javafx.beans.property.Property;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;

public class ICSInfo {
	private IntegerProperty id = new SimpleIntegerProperty(this, "id");
	private StringProperty summary = new SimpleStringProperty(this, "summary");
	private StringProperty description = new SimpleStringProperty(this, "description");
	private Property<Date> startTime = new SimpleObjectProperty<Date>();
	private StringProperty startTimeString = new SimpleStringProperty(this, "startTimeString");
	private Property<Date> endTime = new SimpleObjectProperty<Date>();	
	private StringProperty endTimeString = new SimpleStringProperty(this, "endTimeString");
	private Property<EnumICSInfoType> icsInfoType = new SimpleObjectProperty<EnumICSInfoType>();
	private Property<EnumTaskStatus> status = new SimpleObjectProperty<EnumTaskStatus>();
	private StringProperty statusString = new SimpleStringProperty(this, "statusString");
	private Property<EnumPriority> priority = new SimpleObjectProperty<EnumPriority>();
	private StringProperty priorityString = new SimpleStringProperty(this, "priorityString");
	private IntegerProperty taskid = new SimpleIntegerProperty(this, "taskid");
	private Property<Date> created = new SimpleObjectProperty<Date>();
	private StringProperty createdString = new SimpleStringProperty(this, "createdString");
	
	// id
	public IntegerProperty idProperty(){return this.id;}
	public int getId(){return this.id.get();}
	public void setId(int id){this.id.set(id);}
	
	// summary
	public StringProperty summaryProperty(){return this.summary;}
	public String getsummary(){return this.summary.get();}
	public void setSummary(String summary){this.summary.set(summary);}
	
	// description
	public StringProperty descriptionProperty(){return this.description;}
	public String getDescription(){return this.description.get();}
	public void setDescription(String description){this.description.set(description);}
	
	// start time
	public Property<Date> startTimeProperty(){return this.startTime;}
	public Date getStartTime(){return this.startTime.getValue();}
	public void setStartTime(Date startTime){this.startTime.setValue(startTime);}
	
	
	// start time string
	public StringProperty startTimeStringProperty(){return this.startTimeString;}
	public String getStartTimeString(){return this.startTimeString.get();}
	public void setStartTimeString(String startTimeString){this.startTimeString.set(startTimeString);}
	
	// end time
	public Property<Date> endTimeProperty(){return this.endTime;}
	public Date getEndTime(){return this.endTime.getValue();}
	public void setEndTime(Date endTime){this.endTime.setValue(endTime);}
	
	// start time string
	public StringProperty endTimeStringProperty(){return this.endTimeString;}
	public String getEndTimeString(){return this.endTimeString.get();}
	public void setEndTimeString(String endTimeString){this.endTimeString.set(endTimeString);}
		
	// info type
	public Property<EnumICSInfoType> icsInfoTypeProperty(){return this.icsInfoType;}
	public EnumICSInfoType getICSInfoType(){return this.icsInfoType.getValue();}
	public void setICSInfoType(EnumICSInfoType icsInfoType){this.icsInfoType.setValue(icsInfoType);}
	
	// status 
	public Property<EnumTaskStatus> taskStatusProperty(){return this.status;}
	public EnumTaskStatus getStatus(){return this.status.getValue();}
	public void setStatus(EnumTaskStatus status){this.status.setValue(status);}
	
	// status string
	public StringProperty statusStringProperty(){return this.statusString;}
	public String getStatusString(){return this.statusString.get();}
	public void setStatusString(String statusString){this.statusString.set(statusString);}
	
	// priority
	public Property<EnumPriority> priorityStatusProperty(){return this.priority;}
	public EnumPriority getPriority(){return this.priority.getValue();}
	public void setPriority(EnumPriority priority){this.priority.setValue(priority);}

	//priority string
	public StringProperty priorityStringProperty(){return this.priorityString;}
	public String getPriorityString(){return this.priorityString.get();}
	public void setPriorityString(String priorityString){this.priorityString.set(priorityString);}
	
	//task-id
	public IntegerProperty taskidProperty(){return this.taskid;}
	public int getTaskId(){return this.taskid.get();}
	public void setTaskId(int taskid){this.taskid.set(taskid);}
	
	// created
	public Property<Date> createdProperty(){return this.created;}
	public Date getCreated(){return this.created.getValue();}
	public void setCreated(Date created){this.created.setValue(created);}
	
	public StringProperty createdStringProperty(){return this.createdString;}
	public String getCreatedString(){return this.createdString.get();}
	public void setCreatedString(String createdString){this.createdString.set(createdString);}

	public ICSInfo(){
		this.startTime.addListener(new ChangeListener<Date>(){

			@Override
			public void changed(ObservableValue<? extends Date> arg0,
					Date arg1, Date arg2) {
				// TODO Auto-generated method stub
				updateStartTimeString(arg2);
			}
		});

		this.endTime.addListener(new ChangeListener<Date>(){

			@Override
			public void changed(ObservableValue<? extends Date> arg0,
					Date arg1, Date arg2) {
				// TODO Auto-generated method stub
				updateEndTimeString(arg2);
			}
		});

		this.created.addListener(new ChangeListener<Date>(){

			@Override
			public void changed(ObservableValue<? extends Date> arg0,
					Date arg1, Date arg2) {
				// TODO Auto-generated method stub
				updateCreatedString(arg2);
			}
		});

		
	}
	private void updateStartTimeString(Date startTime){
		// TODO: Use constants instead of hard-coded strings
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		this.setStartTimeString(df.format(startTime));
				
	}
	
	private void updateEndTimeString(Date endTime){
		// TODO: Use constants instead of hard-coded strings
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		this.setEndTimeString(df.format(endTime));
				
	}
	
	private void updateCreatedString(Date created){
		// TODO: Use constants instead of hard-coded strings
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		this.setCreatedString(df.format(created));
	}
}
